/*
 *  Class name: PageableListItemButton.java
 *  Version: 1.0
 *  Date: 11.12.2013
 */
package cz.muni.fi.pa165.soccergamesarchive.restclient.gui.components.pageablelist.items;

import cz.muni.fi.pa165.soccergamesarchive.restclient.gui.components.interfaces.ResultListenable;
import cz.muni.fi.pa165.soccergamesarchive.restclient.gui.components.pageablelist.PageableListPanel;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseListener;
import java.util.Objects;
import javax.swing.JButton;

/**
 *
 * @author Michal Ďuriš
 * @param <T>
 */
public abstract class PageableListItemButton<T> extends JButton implements
        MouseListener {

    public static final Color SELECTED_ITEM_COLOR = new Color(0, 152, 255);
    public static final Color DEFAULT_ITEM_COLOR = new Color(240, 240, 240);

    private static final long serialVersionUID = 1L;

    protected PageableListPanel<T> parentList;
    protected T entity;

    protected boolean selectedItem = false;

    public PageableListItemButton(PageableListPanel<T> parentList, T entity) {
        super();

        this.parentList = parentList;
        this.entity = entity;

        setGlobalSettings();
    }

    public abstract void init();

    public abstract void setEntity(T entity);

    public abstract String getEntityText();

    @Override
    public String getText() {

        setText(entity == null ? "" : getEntityText());

        return super.getText();
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 89 * hash + Objects.hashCode(this.entity);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {

        if (obj == null) {
            return false;
        }

        if (!(obj instanceof PageableListItemButton<?>)) {
            return false;
        }

        final PageableListItemButton<?> other = (PageableListItemButton<?>) obj;

        return Objects.equals(this.entity, other.entity);
    }

    @Override
    public String toString() {
        return getText();
    }

    private void setGlobalSettings() {
        this.addMouseListener(this);
        setMaximumSize(new Dimension(4096, getMaximumSize().height));
        setToolTipText("<html> Left mouse click - change item <br> "
                + "Right mouse click - remove item");
    }

    public T getEntity() {
        return entity;
    }

    public PageableListPanel<T> getParentList() {
        return parentList;
    }

    public void store(ResultListenable requestSourceComponent) {
        parentList.addListItem(this, requestSourceComponent);
    }

    public void change(PageableListItemButton<T> newItemValue,
                       ResultListenable requestSourceComponent) {
        parentList.changeListItem(this, newItemValue,
                requestSourceComponent);
    }

    public void selectItem() {

        selectedItem = true;

        setBackground(SELECTED_ITEM_COLOR);
    }

    public void unselectItem() {

        selectedItem = false;

        setBackground(DEFAULT_ITEM_COLOR);
    }

    public boolean isSelectedItem() {
        return selectedItem;
    }

}
